package com.tzbank.common.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tzbank.common.dao.CommonMapper;
import com.tzbank.common.entity.dto.DictDTO;
import com.tzbank.common.service.CommonService;
import com.tzbank.entity.Area;
import com.tzbank.entity.Bank;
import com.tzbank.entity.Config;
import com.tzbank.redis.RedisUtils;
import com.tzbank.utils.CacheUtil;
import com.tzbank.utils.CodeUtil;
import com.tzbank.utils.ResultCode;
import com.tzbank.utils.ResultUtil;



@Service(value = "commonService")
@SuppressWarnings({ "rawtypes", "unchecked" })
public class CommonServiceImpl implements CommonService {

	@Autowired
    private CommonMapper commonMapper;

	/**
	 * 获取配置列表
	 */
	@Override
	public Map<String,Object> getListConfig() {
		String config =  RedisUtils.getValue("config");		//从redis中获取字典集合
		if(config == null || config.equals("{}")) {
			initDict();			//进行字典和配置获取和格式化放入redis
		}
		config =  RedisUtils.getValue("config");		//从redis中获取字典集合
		Map<String,Object> map_config= (Map<String,Object>) JSONObject.parseObject(config);
		return map_config;
	}
	
	/**
	 * 获取字典集合
	 */
	@Override
	public Map<String,Object> getListDict() {
		String dict =  RedisUtils.getValue("dict");		//从redis中获取字典集合
		if(dict == null || dict.equals("{}")) {
			initDict();		//进行字典和配置获取和格式化放入redis
		}
		dict =  RedisUtils.getValue("dict");
    	Map<String,Object> map_dict= (Map<String,Object>) JSONObject.parseObject(dict);
		return map_dict;
	}
	
	/**
	 * 获取银行列表
	 */
	public List<Bank> getListBank(){
		return commonMapper.getListBank();
	}
	
	/**
	 * 获取热门银行列表
	 */
	public List<Bank> getListHotBank(){
		String bank = RedisUtils.getValue("hotBank");		//从redis中获取热门银行集合
		List list = null;
		if(bank == null) {
			list = commonMapper.getListHotBank();
			RedisUtils.setKey("hotBank", JSONArray.toJSONString(list));		//数据集合放入redis中
			return list;
		}else {
			list = JSONArray.parseArray(bank);		//redis 数据取出来转JSONARR
		}
		return list;
	}
	
	/**
	 * 字典配置初始化
	 */
	public void initDict() {
		// 字典集合
		List<DictDTO> list_dict = commonMapper.getListDict();
		// 配置集合
		List<Config> list_config = commonMapper.getListConfig();

		Map<String, String> map_config = new HashMap<String, String>(); // 配置集合对象
		Map<String, Object> map_dict = CacheUtil.getMapDict(); // 字典集合对象

		// 配置信息放入静态MAP对象
		for (Config c : list_config) {
			map_config.put(c.getConfigCode(), c.getConfigValue());
		}

		// 获取字典集合
		Set<String> set_dict_type = new HashSet<String>();
		for (DictDTO d : list_dict) {
			set_dict_type.add(d.getDictTypeCode());
		}

		for (String type : set_dict_type) {
			JSONArray jsonArr = new JSONArray();

			Config defConfig = null;
			// 匹配是否满足配置表中的
			for (Config c : list_config) {
				// 存放这个
				if (c.getConfigCode().equals(type)) {
					defConfig = c;
				}
			}

			for (DictDTO d : list_dict) {
				// 找到该type下的所有字典集合
				if (type.equals(d.getDictTypeCode())) {
					JSONObject jsonObject = new JSONObject();
					jsonObject.put("text", d.getDictName());
					jsonObject.put("dictValue", d.getDictValue());
					jsonObject.put("defaultFlag", 0);
					// 匹配设置默认值
					if (defConfig != null) {
						if (defConfig.getConfigValue().equals(d.getDictValue())) {
							jsonObject.put("defaultFlag", 1);
						}
					}
					jsonArr.add(jsonObject);
				}
			}
			map_dict.put(type, jsonArr); // 存放各个字典类型的字典数据
		}

		// 把配置 和字典集合放入redis
		RedisUtils.setKey("dict", JSONObject.toJSONString(map_dict));			//对字典进行格式化后放入redis
		RedisUtils.setKey("config", JSONObject.toJSONString(map_config));		//对配置进行格式化后放入redis
	}
	
	/**
     * 获取区域集合
     * @return
     */
	public Map<String,Object> getListArea(){
		Map<String,Object> data = new HashMap<String,Object>();		//数据集合
		List<Area> list =  commonMapper.getListArea();		//db获取区域数据    区域数据app固定时间全量去一次放本地
		data.put("area", list);
		return ResultUtil.resultMap(CodeUtil.CODE_200.getName(), CodeUtil.CODE_200.getCode(), data);
	}

	
	/**
	 * 获取营业网点
	 * @param reqBody
	 * @return
	 */
	/**
	 * 获取网点数据
	 */
	
	public Map<String, Object> getOutlets(Map<String, String> reqBody) {
		
			Map<String, Object>  value = new HashMap<String, Object>();
						List queryProv = commonMapper.queryProv();
						List kk= new ArrayList();	
						for (int h= 0;h < queryProv.size(); h++) {	
							List a= new ArrayList();
							Map<String, Object>  provMap = new HashMap<String, Object>();
							Map mapp = (Map) queryProv.get(h);	
							provMap.put("name", mapp.get("province"));								
										String  pro= (String) mapp.get("province");		
											List queryProvList = commonMapper.queryProvList(pro);
												for (int i = 0; i < queryProvList.size(); i++) {							
													Map<String, Object>  proMap = new HashMap<String, Object>();
													Map map = (Map) queryProvList.get(i);	
													proMap.put("name", map.get("city"));
													String  city= (String) map.get("city");							
														List queryCityList = commonMapper.queryCityList(city);
															List b= new ArrayList();									
															for(int x = 0; x < queryCityList.size(); x++) {
															//	List cc= new ArrayList();		
																Map c = (Map) queryCityList.get(x);	
																Map<String, Object>  areaMap = new HashMap<String, Object>();									
																areaMap.put("name", c.get("areaname"));
																String area_name  = (String) c.get("areaname");	
																List queryCountyList = commonMapper.queryCountyList(area_name);
																List yy= new ArrayList();	
																  for(int y = 0; y < queryCountyList.size(); y++) {
																		Map hh = (Map) queryCountyList.get(y);													
																		Map<String, Object>  CountyMap = new HashMap<String, Object>();			
																		CountyMap.put("name",hh.get("arearoad"));												
																			String area_road  = (String) hh.get("arearoad");	
																				List queryBranchList = commonMapper.queryBranchList(area_road);
																				List ff= new ArrayList();														
																				for(int z= 0; z < queryBranchList.size(); z++) {
																					Map<String, Object>  branchMap = new HashMap<String, Object>();			
																					Map vv = (Map) queryBranchList.get(z);													
																					branchMap.put("dotId",vv.get("branchId"));
																					branchMap.put("name",vv.get("branchname"));
																					branchMap.put("add",vv.get("branchaddress"));
																					ff.add(branchMap);
																				}
																				CountyMap.put("dot", ff);
																			yy.add(CountyMap);
																   }
																    areaMap.put("town",yy );		
																//   cc.add(areaMap);
																	b.add(areaMap);
																	}				
													proMap.put("area", b);							
													a.add(proMap);		
													}			
												provMap.put("city", a);	
												kk.add(provMap);	
											}
						
						value.put("queryResult", kk);
						return ResultUtil.resultMap(ResultCode.RESULT_CODE_SUCCESS.desc(), ResultCode.RESULT_CODE_SUCCESS.code(), value);
		
	}

	/**
	 * 
	 * @param subordinate
	 *            下级
	 * @param superior
	 *            上级
	 */
	public void toDeal(Map<String, Object> subordinate, Map<String, Object> superior) {
		for (Map.Entry<String, Object> entry : superior.entrySet()) {

			List s = (List) entry.getValue();
			List n = new ArrayList();
			Map m = new HashMap();
			for (int i = 0; i < s.size(); i++) {
				m.put(s.get(i), (List) subordinate.get(s.get(i)));
			}
			n.add(m);
			entry.setValue(n);
		}
	}
	
}
